package cn.tedu.hospital.admin.server.patient.service.impl;


import cn.tedu.hospital.admin.server.common.ex.ServiceException;
import cn.tedu.hospital.admin.server.common.pojo.vo.PageData;
import cn.tedu.hospital.admin.server.common.web.ServiceCode;
import cn.tedu.hospital.admin.server.patient.dao.persist.repository.IPatientDetailRepository;
import cn.tedu.hospital.admin.server.patient.dao.persist.repository.IPatientRepository;
import cn.tedu.hospital.admin.server.patient.pojo.entity.Patient;
import cn.tedu.hospital.admin.server.patient.pojo.entity.PatientDetail;
import cn.tedu.hospital.admin.server.patient.pojo.param.PatientAddNewParam;
import cn.tedu.hospital.admin.server.patient.pojo.param.PatientUpdateParam;
import cn.tedu.hospital.admin.server.patient.pojo.vo.PatientListItemVO;
import cn.tedu.hospital.admin.server.patient.pojo.vo.PatientStandardVO;
import cn.tedu.hospital.admin.server.patient.service.IPatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 处理患者数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@Service
public class PatientServiceImpl implements IPatientService {

    @Value("${tea-store.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IPatientRepository patientRepository;

    @Autowired
    private IPatientDetailRepository patientDetailRepository;


    public PatientServiceImpl() {
        log.debug("创建业务类对象：ArticleServiceImpl");
    }

    @Override
    public void addNew(PatientAddNewParam patientAddNewParam) {
        log.debug("开始处理【添加患者】的业务，参数：{}",  patientAddNewParam);

        // TODO 【检查类别是否存在】调用categoryRepository根据ID查询，如果结果为null则抛异常
        // TODO 【类别是否禁用】判断以上查询结果的enable，如果为0则抛异常
        // TODO 【检查是否非父级】判断以上查询结果的isParent，如果为1则抛异常

        Patient patient = new Patient();
        BeanUtils.copyProperties(patientAddNewParam, patient);
        int rows = patientRepository.insert(patient);
        if (rows != 1) {
            String message = "添加患者失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }

        PatientDetail patientDetail = new PatientDetail();
        patientDetail.setPatientId(patient.getPatientId());
        patientDetail.setDiseaseDetail(patientAddNewParam.getDiseaseDetail());
        rows = patientDetailRepository.insert(patientDetail);
        if (rows != 1) {
            String message = "添加病症详情失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }
    @Override
    public void updateInfoById(PatientUpdateParam patientUpdateParam) {
        log.debug("开始处理【修改标签】的业务，参数：{}", patientUpdateParam);

        Long id = patientUpdateParam.getId();
        PatientStandardVO currentPatient = patientRepository.getStandardById(id);
        if (currentPatient == null ) {
            String message = "修改患者失败，尝试修改的患者数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }



        Patient patient = new Patient();
        BeanUtils.copyProperties(patientUpdateParam, patient);
        int rows = patientRepository.update(patient);
        if (rows != 1) {
            String message = "修改患者失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }





    @Override
    public PatientStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询患者详情】的业务，参数：{}", id);
        PatientStandardVO queryResult = patientRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "查询患者详情失败，患者数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<PatientListItemVO> list(Integer pageNum) {
        log.debug("开始处理【查询患者列表】的业务，页码：{}", pageNum);
        PageData<PatientListItemVO> pageData = patientRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<PatientListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询患者列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<PatientListItemVO> pageData = patientRepository.list(pageNum, pageSize);
        return pageData;
    }


}